// pages/memo/memo.js
Page({
  data: {
    isNull: true, //随笔是否为空
    isLayOpen: false,
    Mleft: '',
    Mright: '40px',
    Mtop: '',
    Mbottom: '40px',
    isOperate: false, //是否为文件操作状态
    memoWidth: '100%', //便签默认宽度
    hasMore: false,
    loadText: '加载中...',
    memoWrapperHeight:'',  //便签列表父盒子高度
    memoList: [ //最近
      {
        content:  "新增1",
        date: '10/26',
        isCheck: false
      },
      {
        content: "测试一下",
        date: '10/26',
        isCheck: false
      },
      {
        content: "新增2",
        date: '2019-12-06 22:54:25',
        isCheck: false
      },
      {
        content: "新增3",
        date: '10/26',
        isCheck: false
      },
      {
        content: "新增1",
        date: '10/26',
        isCheck: false
      },
      {
        content: "新增1",
        date: '2019-12-06 22:54:25',
        isCheck: false
      },
      {
        content: "新增1",
        date: '10/26',
        isCheck: false
      },
      {
        content: "新增1",
        date: '10/26',
        isCheck: false
      },
      {
        content: "新增1",
        date: '10/26',
        isCheck: false
      }
    ],
  },
  onLoad: function(options) {
   
  },
  // 拖拽开始
  startMove() {
    //拖拽控制lock
    this.lock = true;
  },
  // 拖拽中
  moving(e) {
    if (this.lock) {
      this.setData({
        Mleft: e.touches[0].clientX - 30 + 'px',
        Mright: '',
        Mtop: e.touches[0].clientY - 30 + 'px',
        Mbottom: ''
      })
    }
  },
  // 拖拽结束
  endMove() {
    this.lock = false;
    this.setData({
      Mleft: '',
      Mright: '40px',
      Mtop: '',
      Mbottom: '40px',
    });
    wx.navigateTo({
      url: '../../pages/addmemo/addmemo?title=新建便签&status=create',
    })
  },
  // 查看便签
  toDisplay(e) {
    let idx = e.currentTarget.dataset.idx;
    // 非文件操作状态 跳转到展示页面
    if (!this.data.isOperate) {
      wx.navigateTo({
        url: '../../pages/addmemo/addmemo?title=随笔&status=display&idx=' + idx,
      });
      // 文件操作状态 
    } else {
        // 文件选中或取消
      this.data.memoList[idx].isCheck = !this.data.memoList[idx].isCheck;
      this.setData({
        memoList: this.data.memoList
      })
    }
  },
  // 操作便签
  toOperate() {
    // 设置便签操作状态
    this.setData({
      isOperate: true,
      memoWidth: '86%'
    })
    // 避免底部操作栏遮挡标签列表
    let h = null;
    let query0=wx.createSelectorQuery();
    query0.select('#memoWrapper').boundingClientRect();
    query0.exec(res=>{
       h = res[0].height;
    })
    let query1 = wx.createSelectorQuery();
    query1.select('#operateGroup').boundingClientRect();
    query1.exec(res => {
       this.setData({
         memoWrapperHeight: h + res[0].height + 'px'
       })
    })
  },
  // 取消操作
  cancleOperate() {
    // 避免底部操作栏遮挡标签列表
    let h = null;
    let query0 = wx.createSelectorQuery();
    query0.select('#memoWrapper').boundingClientRect();
    query0.exec(res => {
      h = res[0].height;
    })
    let query1 = wx.createSelectorQuery();
    query1.select('#operateGroup').boundingClientRect();
    query1.exec(res => {
      this.setData({
        memoWrapperHeight: h - res[0].height + 'px'
      })
    })
    // 文件全选状态置空
    this.isAll = null;
    // 取消便签操作状态 恢复样式
    this.setData({
      isOperate: false,
      memoWidth: '100%'
    });
    // 清空所用选中
    this.data.memoList.forEach(function(item, index) {
      item.isCheck = false
    });
    this.setData({
      memoList:this.data.memoList
    })
  },
  // 全选或取消
  toSelectAll() {
      if (!this.isAll) {
        this.data.memoList.forEach(item=>{
          item.isCheck = true;
        })
        this.isAll = true;
        //  取消选中置顶和非置顶
      } else {
        this.data.memoList.forEach(item=>{
          item.isCheck = false;
        })
        this.isAll = false;
      }
      this.setData({
        memoList: this.data.memoList
      });
      //  分组页选中/取消
  },
  // 删除选中
  toDelete(){
     let deleteObj= {
       deleteItem:()=>{
         this.data.memoList.forEach((item,index,arr)=>{
             if(item.isCheck){
               arr.splice(index,1);
               this.setData({
                 memoList: this.data.memoList
               });
               deleteObj.deleteItem();
             }
         })
       }
     }
     deleteObj.deleteItem();
  },
  // 触底事件
  onReachBottom() {
    if(this.data.memoList.length>0 && !this.data.isOperate){
      this.setData({
        hasMore: true
      })
      this.data.memoList.forEach(item => {
        this.data.memoList.push(item);
      })
      setTimeout(() => {
        this.setData({
          memoList: this.data.memoList
        });
      }, 1000)
    }
    },
      //  下拉事件
  onPullDownRefresh: function () {
    setTimeout(() => {
      wx.stopPullDownRefresh();
      wx.showToast({
        title: "刷新完成",
      });
    }, 800)
  }
})